import 'package:flutter/material.dart';
import 'package:flutter_ability/custom_ui.dart';

/// Text封装
class TextView {
  TextView._();

  static double line = YJSize.dp(8);

  static String RubikMedium = "RubikMedium";
  static String RubikRegular = "RubikRegular";
  static String RubikMediumItalic = "RubikMediumItalic";

  /// Text封装
  /// @param  fontFamily 字体类型 :TextView.PingFang
  /// @param color 字体颜色
  /// @param fontSize 字体大小
  /// @param fontWeight 字体粗细: 【粗体fontWeight:FontWeight.w600】
  /// @param strutHeight 文字高度 【字体居中strutHeight:1.3】
  /// @param overflows 超出一行... maxLines: 1,
  ///                            overflows: TextOverflow.ellipsis
  /// @param textScaleFactor    1.0
  /// @param TextDecoration 可在文字上 中 下方添加直线[下划线、 删除线]
  /// @param lineHeight 设置文本行高 计算公式：「line-height = font size+8」
  /// @param isBold 是否粗体
  static Widget text(
    String? data, {
    String? fontFamily = "",
    Color? color = YJColors.BLACK85,
    double? fontSize = 16,
    TextAlign? textAlign = TextAlign.left,
    AlignmentGeometry? alignment = Alignment.centerLeft,
    FontWeight? fontWeight,
    double? strutHeight = 0.0,
    int? maxLines,
    TextOverflow? overflows,
    bool? isLineHeight = true,
    double? textScaleFactor,
    TextDecoration? decoration,
    TextDecorationStyle? decorationStyle,
    Color? decorationColor,
    double? height,
    double? lineHeight,
    bool? isBold = false,
  }) {
    if (data == null || data.isEmpty) {
      return const Text("");
    }
    StrutStyle? strutStyle = null;
    if (strutHeight != null && strutHeight != 0.0) {
      strutStyle =
          StrutStyle(leading: 0, height: strutHeight, forceStrutHeight: true);
    }

    /// 设置行高 算法:Size+8dp(因为size越大 默认1.5倍的行高越大，所以固定8dp)
    /// 14 14+8 = 22/14 = 1.57
    /// 16 16+8 = 24/16 = 1.50
    /// 18 18+8 = 26/18 = 1.44
    if (isLineHeight == true && fontSize != null) {
      var lHeight = lineHeight ?? YJSize.dp(8);
      height = (fontSize + lHeight) / fontSize;
    }
    return _textWidget(
        data,
        textAlign,
        strutStyle,
        maxLines,
        overflows,
        textScaleFactor,
        fontFamily,
        fontWeight,
        fontSize,
        decoration,
        decorationStyle,
        decorationColor,
        color,
        height,
        isBold);
  }

  static Text _textWidget(
    String data,
    TextAlign? textAlign,
    StrutStyle? strutStyle,
    int? maxLines,
    TextOverflow? overflows,
    double? textScaleFactor,
    String? fontFamily,
    FontWeight? fontWeight,
    double? fontSize,
    TextDecoration? decoration,
    TextDecorationStyle? decorationStyle,
    Color? decorationColor,
    Color? color,
    double? height,
    bool? isBold,
  ) {
    return Text(data,
        textAlign: textAlign,
        strutStyle: strutStyle,
        maxLines: maxLines,
        overflow: overflows,
        textScaleFactor: textScaleFactor,
        style: TextStyle(
            fontFamily: fontFamily,
            fontWeight: fontWeight != null
                ? fontWeight
                : isBold == true
                    ? FontWeight.w600
                    : FontWeight.w400,
            height: height,
            leadingDistribution: TextLeadingDistribution.even,
            fontSize: fontSize,
            decoration: decoration,
            decorationStyle: decorationStyle,
            decorationColor: decorationColor,
            color: color));
  }
}
